package main;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import dao.FileIO;
import dao.PropertyIO;

public class PositiveSampleAnalyzer {

	public void proceed(Properties properties) {
		String filename = properties.getProperty("input_file");
		String outputfile = properties.getProperty("output_file");
		String outputfile2 = properties.getProperty("output_file2");
		
		List<String> fileList = FileIO.readFile(filename);
		
		List<Integer> allBlocks = new LinkedList<Integer>();
		
		List<String> list = new LinkedList<String>();
		for(String file : fileList) {
			List<Integer> blocks = getBlocks(file);
			allBlocks.addAll(blocks);
			list.add(file + "\t" + getString(blocks) + "\n");
		}
		
		FileIO.writeFile(outputfile, list);
		
		FileIO.writeFile(outputfile2, Arrays.asList(getString(allBlocks).replace("\t", "\n") + "\n"));
	}
	
	protected String getString(List<Integer> blocks) {
		StringBuilder sb = new StringBuilder();
		for(int block : blocks) {
			sb.append(block + "\t");
		}
		return sb.toString().trim();
	}

	protected List<Integer> getBlocks(String filename) {
		List<String> lines = FileIO.readFile(filename);

		List<Integer> sizeList = new LinkedList<Integer>();
		int index = 0;
		int state = 0;
		int counter = 0;
		for (String line : lines) {
			if (state == 0) {
				if (line.compareTo("1") == 0) {
					state = 1;
					counter++;
				}
			} else {
				if (line.compareTo("0") == 0) {
					state = 0;
					sizeList.add(counter);
					counter = 0;
				} else {
					counter ++;
				}
			}
			index++;
		}
		
		if(state == 1) {
			sizeList.add(counter);
		}
		
		return sizeList;
	}
	
	public static void main(String[] args) {
		if(args.length != 1) {
			return;
		}
		
		Properties properties = PropertyIO.load(args[0]);
		PositiveSampleAnalyzer psa = new PositiveSampleAnalyzer();
		psa.proceed(properties);
	}

}
